from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.decorators import permission_required
from ..models import Order,Users,OrderInfo,Address
# Create your views here.
# 网站的后台首页
def index(request):
    
    return render(request,'admin/index.html')


# 订单列表
@permission_required('myadmin.show_order',raise_exception = True)
def orderlist(request):
    # 获取数据
    users = Users.objects.exclude(status=2)
    orderlist = Order.objects.all()
    orderinfolist = OrderInfo.objects.all()
    address = Address.objects.all()
    # 接受参数 
    types = request.GET.get('types',None)
    keywords = request.GET.get('keywords',None)
    # 判断是否右搜索条件
    if types:
        if types == 'all':
            # 多字段的查询
            # select * from users where username like '%aa%' or age like '%aa%' or
            from django.db.models import Q
            orderlist = orderlist.filter(Q(id__contains=keywords)|Q(username__contains=keywords)|Q(phone__contains=keywords))
        else:   
            
            data ={types+'__contains':keywords}
            orderlist = orderlist.filter(**data)
     # 数据分页
    from django.core.paginator import Paginator
    # 实例化分页对象,参数1,数据列表  参数2 每页显示的条数
    p = Paginator(orderlist, 10)
    # 获取当前的页码数
    page = request.GET.get('page',1)
    # 获取当前页的数据
    orderlist = p.page(page)
    # 获取所有的页码列表
    pages = p.page_range
    context = {'users':users,'orderlist':orderlist,'orderinfolist':orderinfolist,'address':address}

    # return render(request,'myadmin/goods/order.html',context)

    return render(request,'admin/orders/orderlist.html',context)
